<?php

namespace App\Service;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;


class ReqService
{
    public static function reqPostJson($data,$url)
    {
        $UrlInfo = parse_url($url);
        $domain = $UrlInfo['scheme'] . '://'.$UrlInfo['host'];
        $options = ['base_uri' => $domain];

        // $client 为协程化的 GuzzleHttp\Client 对象
        $client = (new Client($options));

//        $urlPath= ;

        $urlPath = isset($UrlInfo['query']) ? $UrlInfo['path']. '?' . $UrlInfo['query'] : $UrlInfo['path'];

//        log_info(__CLASS__.':==:'.$domain.$urlPath);

        $response = $client->request('POST', $urlPath, [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'body' => json_encode($data)
        ]);

        $result = $response->getBody()->getContents();
//        log_info(__CLASS__.':'.__FUNCTION__.'=='.$result);
        return $result;
    }

    public static function reqGet($url)
    {
        $client = (new Client());

        try {
            $response = $client->get($url);
        } catch (GuzzleException $e) {
            log_error(__CLASS__.':'.__FUNCTION__.'=='.$e->getMessage());
            return false;
        }

        $result = $response->getBody()->getContents();
        log_info(__CLASS__.':'.__FUNCTION__.'=='.$result);
        return $result;
    }

}